[Clippy] Address clippy::unnecessary_cast in production code#1238
Draft
DanielEScherzer wants to merge 3 commits into
Draft
[Clippy] Address clippy::unnecessary_cast in production code#1238DanielEScherzer wants to merge 3 commits into
clippy::unnecessary_cast in production code#1238DanielEScherzer wants to merge 3 commits into
Conversation
Contributor
Author
|
This is the first of a few PRs to get clippy passing, and then I'll send a PR to add it to CI |
weihanglo
reviewed
May 8, 2026
This comment has been minimized.
This comment has been minimized.
weihanglo
reviewed
May 13, 2026
This was referenced Jun 11, 2026
DanielEScherzer
added a commit
to DanielEScherzer/git2-rs
that referenced
this pull request
Jun 15, 2026
Add clippy to CI so that future changes do not increase the number of violations. The existing violations are allowlisted on a module level (i.e. at the top of files with inner attributes, or above the start of nested inline test modules with outer attributes). CI runs clippy on both Window and Linux because some lints may fire on one but not the other; see, e.g. rust-lang#1238. Existing violations will be addressed in subsequent pull requests.
DanielEScherzer
added a commit
to DanielEScherzer/git2-rs
that referenced
this pull request
Jun 15, 2026
Add clippy to CI so that future changes do not increase the number of violations. The existing violations are allowlisted on a module level (i.e. at the top of files with inner attributes, or above the start of nested inline test modules with outer attributes). CI runs clippy on both Window and Linux because some lints may fire on one but not the other; see, e.g. rust-lang#1238. Existing violations will be addressed in subsequent pull requests.
DanielEScherzer
added a commit
to DanielEScherzer/git2-rs
that referenced
this pull request
Jun 15, 2026
Add clippy to CI so that future changes do not increase the number of violations. The existing violations are allowlisted on a module level (i.e. at the top of files with inner attributes, or above the start of nested inline test modules with outer attributes). CI runs clippy on both Window and Linux because some lints may fire on one but not the other; see, e.g. rust-lang#1238. Existing violations will be addressed in subsequent pull requests.
DanielEScherzer
added a commit
to DanielEScherzer/git2-rs
that referenced
this pull request
Jun 15, 2026
Add clippy to CI so that future changes do not increase the number of violations. The existing violations are allowlisted on a module level (i.e. at the top of files with inner attributes, or above the start of nested inline test modules with outer attributes). CI runs clippy on both Window and Linux because some lints may fire on one but not the other; see, e.g. rust-lang#1238. Existing violations will be addressed in subsequent pull requests.
DanielEScherzer
added a commit
to DanielEScherzer/git2-rs
that referenced
this pull request
Jun 15, 2026
Add clippy to CI so that future changes do not increase the number of violations. The existing violations are allowlisted on a module level (i.e. at the top of files with inner attributes, or above the start of nested inline test modules with outer attributes). CI runs clippy on both Window and Linux because some lints may fire on one but not the other; see, e.g. rust-lang#1238. Existing violations will be addressed in subsequent pull requests.
DanielEScherzer
marked this pull request as draft
June 15, 2026 18:20
DanielEScherzer
force-pushed
the
clippy-unnecessary_cast
branch
from
July 19, 2026 20:01
66ebc7d to
08e5e75
Compare
clippy::unnecessary_cast lint in src/clippy::unnecessary_cast in production code
In a lot of places, the lint is triggered when casting a constant from the libgit2 bindings that is created (in libgit2-sys) via `git_enum!`. That macro will use i32 when `#[cfg(target_env = "msvc")]`, and u32 otherwise, meaning that a number of the casts that clippy flags as "unnecessary" are indeed necessary. In those cases, use `#[allow]` with a reason. Similarly, clippy also lints against casting of `c_int` and `c_uint` to `i32` and `u32` - while those are usually the same types, the libc documentation makes it clear that they are not always the same. In those cases too, use `#[allow]` with a reason. The only libc-provided type with a consistent rust type is that `size_t` is always the same as `usize` - fix the places that clippy identified where that conversion is unneeded. Other fixes include casts when the underlying type is explicitly set in the libgit2 bindings to be a specific rust type, or when converting mutable pointers to the same type they already are.
DanielEScherzer
force-pushed
the
clippy-unnecessary_cast
branch
from
July 19, 2026 20:10
08e5e75 to
4e982c7
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
In a lot of places, the lint is triggered when casting a constant from the libgit2 bindings that is created (in libgit2-sys) via
git_enum!. That macro will use i32 when#[cfg(target_env = "msvc")], and u32 otherwise, meaning that a number of the casts that clippy flags as "unnecessary" are indeed necessary. In those cases, use#[allow]with a reason.Similarly, clippy also lints against casting of
c_intandc_uinttoi32andu32- while those are usually the same types, the libc documentation makes it clear that they are not always the same. In those cases too, use#[allow]with a reason.The only libc-provided type with a consistent rust type is that
size_tis always the same asusize- fix the places that clippy identified where that conversion is unneeded. Other fixes include casts when the underlying type is explicitly set in the libgit2 bindings to be a specific rust type, or when converting mutable pointers to the same type they already are.